package org.smartercontext.local.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.smartercontext.contextmger.DTO.StudyDTO;
import org.smartercontext.contextmger.DTO.WorkDTO;
import org.smartercontext.contextmger.contextsphere.ContextManagerController;
import org.smartercontext.contextmger.utilities.GetWorkAndStudy;

import com.hp.hpl.jena.rdf.model.Model;

/**
 * Servlet implementation class AddWSServlet
 */
@WebServlet("/AddWSServlet")
public class AddWSServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddWSServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
    	if(request.getQueryString()!=null){
    		HttpSession session = request.getSession();
    		String username = session.getAttribute("username").toString();
    		if(username!=null){
    			
    			int quantity = 0;
    			String typeA = "typeA";
    			String queryCopy = request.getQueryString();
    			while(queryCopy.indexOf(typeA)>-1){
    				queryCopy = queryCopy.substring(queryCopy.indexOf(typeA)+typeA.length(), queryCopy.length());
    				quantity++;
    			}
    			
    			List<StudyDTO>studies = new ArrayList<>();
    			List<WorkDTO>jobs = new ArrayList<>();
    			
    			for(int i=0;i<quantity;i++){
    				String parameterType = typeA+i;
    				String paramName = "nameJAV"+i;
    				String paramTypeS = "typeS"+i;
    				String paramTime = "timeS"+i;
    				String paramLoc = "loc"+i;
    				
    				String type = request.getParameter(parameterType);
    				String name = request.getParameter(paramName);
    				String time = request.getParameter(paramTime);
    				
    				if(name == null){
    					response.sendRedirect("manageContext_W&S.jsp?Error=1");
    				}
    				
    				if(type.equals("S")){
    					
    					String sType = request.getParameter(paramTypeS);
    					StudyDTO dto = new StudyDTO();
    					dto.setSchoolName(name);
    					dto.setSchoolType(sType);
    					
    					if(time.equals("Past")){
    						dto.setEndYear("X");
    					}
    					
    					studies.add(dto);
    					
    				}else{
    					
    					String loc = request.getParameter(paramLoc);
    					WorkDTO wdto = new WorkDTO();
    					wdto.setEmployerName(name);
    					wdto.setEmployerLoc(loc);
    					
    					if(time.equals("Past")){
    						wdto.setEndDate("x");
    					}
    					
    					jobs.add(wdto);
    				}
    			}
    			
    			try {
    				GetWorkAndStudy gwas = new GetWorkAndStudy();
    				gwas.setJobs(jobs);
    				gwas.setStudies(studies);
    				
    				ContextManagerController.contextManagerController();
					Model usermodel = ContextManagerController.getUsermodel(username);
					Model usermodelU = gwas.addWSToUserModel(usermodel, username);
					ContextManagerController.integrateContext(usermodelU, username);
					response.sendRedirect("manageContext_W&S.jsp");
										
				} catch (Exception e) {
					//response.sendRedirect("manageContext_W&S.jsp?Error=1");
					e.printStackTrace();
				}
    			
    		}else{
    			response.sendRedirect("login.jsp?Err=3");
    		}
    	}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
